""" AdminRolePermission Model """

from masoniteorm.models import Model
from app.models.AdminPermission import AdminPermission
from masoniteorm.collection import Collection


class AdminRolePermission(Model):
    """AdminRolePermission Model"""

    @classmethod
    def generate_log(cls, admin_role_id, admin_permission_id):
        """
        生成日志
        @param admin_role_id:
        @param admin_permission_id:
        @return: None
        """
        cls.create({
            'admin_role_id': admin_role_id,
            'admin_permission_id': admin_permission_id
        })

    @classmethod
    def get_permission_ids(cls, admin_role_id):
        """
        获取所有权限 ID
        @param admin_role_id:
        @return: [1, 2, 3, 4]
        """
        admin_role_permission = cls.where('admin_role_id', admin_role_id).get()
        return Collection(admin_role_permission).pluck('admin_permission_id').all()

    @classmethod
    def handle_checked(cls, admin_role_id, data):
        """
        处理角色权限数据，主要处理是否有已选中的选项
        @param admin_role_id:
        @param data:
        @return:
        """
        ids = cls.get_permission_ids(admin_role_id)
        if ids is None:
            return data
        for index, permission in enumerate(data):
            """
            //dump($permission);
            /*array:6 [
                "id"        => 1
                "title"     => "系统设置"
                "field"     => "title"
                "checked"   => false
                "spread"    => false // 是否展开
                "children"  => array:87 [
                    0 => array:6 [
                        "id"        => 7
                        "title"     => "菜单(admin/menu/index)"
                        "uri"       => "admin/menu/index"
                        "pid"       => 1
                        "field"     => "title"
                        "checked"   => false // 是否选中
                    ]
                ]
            ]*/
            """
            data[index]['spread'] = False
            children = data[index]['children']
            if children is not None:
                for index2, child in enumerate(children):
                    """
                    //dump($child);
                    /*array:6 [
                        "id"        => 7
                        "title"     => "菜单(admin/menu/index)"
                        "uri"       => "admin/menu/index"
                        "pid"       => 1
                        "field"     => "title"
                        "checked"   => false
                    ]*/
                    """
                    if child['id'] in ids:
                        data[index]['children'][index2]['checked'] = True
        return data

    @classmethod
    def is_pass(cls, admin_role_id, uri):
        """
        判断角色权限 id 是否有访问权限
        @param admin_role_id:
        @param uri:
        @return:
        """
        admin_permission = AdminPermission.where('uri', uri).first()
        if admin_permission is None:
            return False

        ids = cls.get_permission_ids(admin_role_id)
        return admin_permission.id in ids
